package com.tjrac.generalpermissions.factory;

import com.tjrac.filter.epception.KeyNullException;
import com.tjrac.filter.epception.KeyRepeatException;
import com.tjrac.filter.factory.FilterFactories;
import com.tjrac.filter.factory.PermissionFactories;
import com.tjrac.filter.stereotype.FilterInjection;
import com.tjrac.generalpermissions.dao.PTypeDao;
import com.tjrac.generalpermissions.dao.PermissionDao;
import com.tjrac.generalpermissions.domain.Permission;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;


/**
 * PermissionListener
 *
 * @author : xziying
 * @create : 2020-11-18 10:16
 */
@Component
public class PermissionListener {

    @FilterInjection
    static public PermissionFactories permissionFactories;

    @Resource
    PermissionDao permissionDao;

    // 是否为第一次初始化
    static boolean init = false;

    /**
     * 初始化所有权限
     */
    @PostConstruct
    public void permissionsInit(){

        if (!init) {
            System.out.println("初始化权限对象");
            List<Permission> permissions = permissionDao.queryAll();
            for (Permission permission : permissions){
                try {
                    permissionFactories.writePermission(permission);
                } catch (KeyRepeatException | KeyNullException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
            permissionFactories.setConfigStrict(true);
        }
        init = true;
    }
}
